clear;clc;

nPoints = 50;
scanValue = 1:100; scanNum = length(scanValue);
fastFreq = 10.0; slowFreq = fastFreq/scanNum;
waveform = scanWave(scanValue, nPoints);

%% device
dev=EquipmentControl.AWG.TektronixAFG3252('visa-usb',  'TCPIP0::192.168.1.101::inst0::INSTR');
% dev=EquipmentControl.AWG.TektronixAFG3252('visa-usb',  'USB0::0x0699::0x034E::B010598::0::INSTR');

%% channel 1
ch1Wave = containers.Map({'shape', 'frequency', 'phase', 'amplitude', 'offset'}, ...
                         {'RAMP',   fastFreq,   -90.0,     1.0,         0.0});

ch1Burst = containers.Map({'burst_mode','burst_cycle', 'burst_delay', 'burst_state'}, ...
                          {'TRIG',      scanNum,       0.0,           'ON'});

dev.setWave(1, ch1Wave).setBurst(1, ch1Burst);

%% channel 2
ch2Wave = containers.Map({'shape', 'frequency', 'phase', 'amplitude', 'offset'}, ...
                         {'USER1', slowFreq,    0.0,     1.0,         0.0});

ch2Burst = containers.Map({'burst_mode','burst_cycle', 'burst_delay', 'burst_state'}, ...
                          {'TRIG',      1,              0.0,           'ON'});

dev.setAWGWaveform(waveform, 'USER1');
dev.setWave(2, ch2Wave).setBurst(2, ch2Burst);

%% Trigger
trig = containers.Map({'trig_source','trig_slope', 'trig_interval'},...
                      {'EXT',       'NEG',         1e-3});
dev.setTrigger();


%% NI
ni = EquipmentControl.NIDAQ.NIUSB6366('dev1');

ni.addInputChannel(0).addInputChannel(1);
ni.addExternalTrigger('PFI0', 'StartTrigger');
ni.setSampling(10.0, 50e3);
% ni.addlistener();
ni.setProgress('scan progress: ', scanNum);
ni.startBackground();

%% Run
dev.fire(1).fire(2);
dev.forceTrigger();
ni.wait().stop();

%% clean
dev.silence(1).silence(2);
dev.disconnect_device();

%% Plot
[X,Y] = meshgrid(ni.time(:, 1), scanValue);
Z = ni.data.';
Z1=Z(1:2:end,:);
Z2=Z(2:2:end,:);

figure;
subplot(1,2,1);
contourf(X, Y, Z1, 100, 'LineStyle', 'none');
subplot(1,2,2);
contourf(X, Y, Z2, 100, 'LineStyle', 'none');
pause(1);